package com.lwt.monstermall.cart.controller;

import com.lwt.monstermall.cart.service.CartService;
import com.lwt.monstermall.cart.vo.CartItemVo;
import com.lwt.monstermall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @program: monstermall
 * @author: 大怪
 * @create: 2022-02-27 15:05
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> getCurrentUserCartItems(){
        return cartService.getUserCartItems();
    }


    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.monstermall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.monstermall.com/cart.html";
    }


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("checked") Integer checked){
        cartService.checkItem(skuId,checked);
        return "redirect:http://cart.monstermall.com/cart.html";

    }

    /**
     * 浏览器中有cookie:user-key:标识用户身份，一个月过期
     * 如果第一次使用JD的购物车功能，都会给一个临时的用户身份
     * 浏览器保存以后，每一次访问都会带上这个cookie
     *
     * 登录，Session中有
     * 没登录：按照cookie里面带来的user-key来做
     * 第一次：如果没有临时用户，帮忙创建个临时用户
     * @param model
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model){

        //快速得到用户信息，id，user-key
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra){
       CartItemVo cartItemVo = cartService.addToCart(skuId,num);
       ra.addAttribute("skuId",skuId);
       return "redirect:http://cart.monstermall.com/addToCartSuccess.html";
    }


    /**
     * 跳转到购物车添加成功页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //重定向到成功页面.再次查询购物车数据即可
        CartItemVo itemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",itemVo);
        return "success";
    }

}
